/**
 * *********************** ******* ***********************************
 *
 *  
 * 
 *  
 * *******************************************************************
 */

package com.arges.web.domain.tableutil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 表里面的列
 *
 * 
 * @version Ver 3.0
 * @since Ver 3.0
 * @Date 2012-12-11
 *
 */
public class TableColumn
{
	private String columnName;

	private String propertyName;

	private String pojoPropertyName; // 该属性目前只有DbTableForDataCompareTemp.java使用

	private Method method; // beanClass中对应的属性get或set方法名获取属性值的方法

	private Method pojoMethod; // pojoClass中对应的属性get或set方法名获取属性值的方法,
								// 该属性目前只有DbTableForDataCompareTemp.java使用

	public TableColumn(String columnName, String propertyName)
	{
		this.columnName = columnName;
		this.propertyName = propertyName;
	}

	public TableColumn(String columnName, String propertyName, String pojoPropertyName)
	{
		this.columnName = columnName;
		this.pojoPropertyName = pojoPropertyName;
		this.propertyName = propertyName;
	}

	public String getColumnName()
	{
		return columnName;
	}

	public String getPropertyName()
	{
		return propertyName;
	}

	public String getPojoPropertyName()
	{
		return pojoPropertyName;
	}

	public Method getMethod()
	{
		return method;
	}

	public Method getPojoMethod()
	{
		return pojoMethod;
	}

	@SuppressWarnings("unchecked")
	public static void setBeanClass(Class beanClass, List<TableColumn> cols)
	{
		for (TableColumn col : cols)
		{
			col.method = EnhancedReflectionUtils.getMethodForField(beanClass, col.propertyName);
		}
	}

	@SuppressWarnings("unchecked")
	public static void setPojoClass(Class pojoClass, List<TableColumn> cols)
	{
		for (TableColumn col : cols)
		{
			if (col.pojoMethod == null)
			{
				col.pojoMethod = EnhancedReflectionUtils.getMethodForField(pojoClass, col.pojoPropertyName);
			}
		}
	}

	/**
	 * 将一个list的TableColumn 组装成 一下sql模板 col1 = ?, col2 = ?, col3 = ?
	 * 
	 * @param columns
	 * @param symbol 列名和值的关系，如例子中为 "="
	 * @param seperator 在col1 = ? col2 = ? 之间的分割符，如例子中分隔符为 ","
	 * @return
	 */
	public static String toColSqlTemplate(List<TableColumn> columns, String symbol, String seperator)
	{
		List<String> colNames = new ArrayList<String>();
		for (TableColumn col : columns)
		{
			colNames.add(col.getColumnName() + " " + symbol + " ?");
		}
		return EnhancedStringUtils.makeUp(seperator, colNames);
	}

	/**
	 * 将一个list的TableColumn 组装成 一下sql模板 col1 = ?, col2 = ?, col3 = ?
	 * 
	 * @param columns
	 * @param seperator 在col1 = ? col2 = ? 之间的分割符，如例子中分隔符为 ","
	 * @return
	 */
	public static String toColSqlTemplate(List<TableColumn> columns, String seperator)
	{
		return toColSqlTemplate(columns, " = ", seperator);
	}

	public String toString()
	{
		return columnName;
	}

}
